<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--通用查询-->
      <queryHeader :dict="dict" :permission="permission" />
      <!--通用编辑-->
      <crudOperation :permission="permission" />
    </div>

    <!--表格渲染-->
    <el-table ref="table" v-loading="crud.loading" :data="crud.data" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
      <!--多选框-->
      <el-table-column type="selection" width="55" />
      <!--展示字段-->
      <el-table-column v-if="columns.visible('articleTitle')" min-width="160" prop="articleTitle" label="文章标题" :show-overflow-tooltip="true" />
      <el-table-column v-if="columns.visible('articleSubtitle')" min-width="160" prop="articleSubtitle" label="文章副标题" :show-overflow-tooltip="true" />
      <el-table-column v-if="columns.visible('articleCoverPic')" prop="articleCoverPic" label="文章封面图" min-width="160" :show-overflow-tooltip="true" />
      <el-table-column v-if="columns.visible('artcleIntroduction')" prop="artcleIntroduction" label="文章简介" :show-overflow-tooltip="true" />
      <el-table-column v-if="columns.visible('articleAuthor')" prop="articleAuthor" label="文章作者" min-width="120" :show-overflow-tooltip="true" />
      <el-table-column v-if="columns.visible('articleSource')" prop="articleSource" label="文章来源" min-width="120" :show-overflow-tooltip="true" />
      <el-table-column v-if="columns.visible('articleType')" prop="articleType" label="文章类型 " :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{ dict.label.type_article[scope.row.articleType] }}
        </template>
      </el-table-column>
      <el-table-column v-if="columns.visible('optTime')" prop="optTime" label="上传时间" min-width="150" :show-overflow-tooltip="true" />
      <el-table-column v-if="columns.visible('reviewStatus')" prop="reviewStatus" label="审核状态" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{ dict.label.status_common_review[scope.row.reviewStatus] }}
        </template>
      </el-table-column>
      <el-table-column v-if="columns.visible('publishStatus')" prop="publishStatus" label="发布状态" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{ dict.label.status_article_publish[scope.row.publishStatus] }}
        </template>
      </el-table-column>
      <el-table-column v-if="columns.visible('readTimes')" prop="readTimes" label="浏览次数" :show-overflow-tooltip="true" />

      <!--操作-->
      <el-table-column label="操作" width="200px" align="center" fixed="right">
        <template slot-scope="scope">
          <!--通用编辑删除-->
          <udOperation :data="scope.row" :permission="permission">
            <template slot="left">
              <el-button size="mini" type="text" @click="preview(scope.row)">预览</el-button>
              <el-button size="mini" type="text" @click="publish(scope.row)">发布</el-button>
            </template>
          </udOperation>
        </template>
      </el-table-column>
    </el-table>

    <!--分页组件-->
    <pagination />

    <!--  文章发布   -->
    <el-dialog title="文章发布" :visible.sync="releaseShow">
      <el-checkbox-group v-model="publicSections" v-loading="publicLoading">
        <el-checkbox v-for="role in childSections" :key="role.sectionId" :label="role.sectionId">{{ role.sectionName }}</el-checkbox>
      </el-checkbox-group>

      <div slot="footer" class="dialog-footer">
        <el-button @click="releaseShow = false">
          取消
        </el-button>
        <el-button type="primary" @click="bindSection()">
          提交
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import crudArticle from '@/api/business/common/article/article-list'
import queryHeader from './module/queryHeader'
import CRUD, { presenter } from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import udOperation from '@crud/UD.operation'
import { getChildSections } from '@/api/business/common/article/article-channel'
import { getArticlePublishSections, publish } from '@/api/business/common/article/article-publish'

// crud交由presenter持有
const crud = CRUD({ title: 'ArticleList', url: 'article/list/page', crudMethod: { ...crudArticle }, pkIdName: 'id' })

export default {
  name: 'Article',
  components: { queryHeader, crudOperation, pagination, udOperation },
  dicts: ['type_article', 'status_article_publish', 'status_common_review'],
  mixins: [presenter(crud)],
  data() {
    return {
      childSections: [], publicSections: [], publicLoading: false, releaseShow: false, optArticleId: null,
      // 对应几个按钮操作
      permission: {
        add: [],
        edit: [],
        del: []
      }
    }
  },
  created() {
    this.crud.optShow = { add: true, edit: false, del: true, download: false }
    this.getSections()
  },
  methods: {
    [CRUD.HOOK.beforeToAdd](crud, form) {
      this.$router.push({ path: 'edit', query: { opt: 'create' }})
      return false
    },
    [CRUD.HOOK.beforeToEdit](crud, form) {
      this.$router.push({ path: 'edit', query: { opt: 'edit', id: form.id }})
      return false
    },
    preview(row) {
      this.$router.push({ path: 'preview', query: { id: row.id }})
    },
    getSections() {
      getChildSections().then(res => {
        this.childSections = res
      })
    },
    publish(row) {
      this.optArticleId = row.id
      this.releaseShow = true

      getArticlePublishSections(this.optArticleId).then((res) => {
        this.publicSections = res.map(item => item.sectionId)
      })
    },
    bindSection() {
      this.publicLoading = true
      publish(this.optArticleId, this.publicSections, 1).then((res) => {
        this.publicLoading = false
        this.releaseShow = false
        this.$notify({
          title: 'Success',
          message: '发布成功',
          type: 'success',
          duration: 2000
        })
      }).catch(() => {
        this.publicLoading = false
      })
    },
    treeNormalizer(node) {
      return {
        id: node.sectionId,
        label: node.sectionName,
        children: node.childs
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  /deep/ .el-input-number .el-input__inner {
    text-align: left;
  }
</style>
